package org.hfy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hfy.common.ApiResponse;
import org.hfy.dto.OptionDTO;
import org.hfy.dto.QuotationQueryDTO;
import org.hfy.entity.Quotation;
import org.hfy.service.QuotationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 报价单控制器
 * 
 * @author hfy
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/quotations")
@RequiredArgsConstructor
@Validated
public class QuotationController {

    private final QuotationService quotationService;

    /**
     * 分页查询报价单列表
     */
    @GetMapping
    public ApiResponse<IPage<Quotation>> getQuotationPage(QuotationQueryDTO query) {
        IPage<Quotation> page = quotationService.getQuotationPage(query);
        return ApiResponse.success(page);
    }

    /**
     * 根据ID查询报价单详情
     */
    @GetMapping("/{id}")
    public ApiResponse<Quotation> getQuotationById(@PathVariable Long id) {
        Quotation quotation = quotationService.getQuotationWithItemsById(id);
        return ApiResponse.success(quotation);
    }

    /**
     * 创建报价单
     */
    @PostMapping
    public ApiResponse<Quotation> createQuotation(@Valid @RequestBody Quotation quotation) {
        Quotation result = quotationService.createQuotation(quotation);
        return ApiResponse.success("报价单创建成功", result);
    }

    /**
     * 更新报价单
     */
    @PutMapping("/{id}")
    public ApiResponse<Quotation> updateQuotation(@PathVariable Long id, @Valid @RequestBody Quotation quotation) {
        Quotation result = quotationService.updateQuotation(id, quotation);
        return ApiResponse.success("报价单更新成功", result);
    }

    /**
     * 删除报价单
     */
    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteQuotation(@PathVariable Long id) {
        boolean result = quotationService.deleteQuotation(id);
        if (result) {
            return ApiResponse.success("报价单删除成功");
        } else {
            return ApiResponse.error("报价单删除失败");
        }
    }

    /**
     * 生成合同
     */
    @PostMapping("/{id}/generate-contract")
    public ApiResponse<Long> generateContract(@PathVariable Long id) {
        Long contractId = quotationService.generateContract(id);
        return ApiResponse.success("合同生成成功", contractId);
    }

    /**
     * 提交报价单
     */
    @PostMapping("/{id}/submit")
    public ApiResponse<String> submitQuotation(@PathVariable Long id) {
        boolean result = quotationService.submitQuotation(id);
        if (result) {
            return ApiResponse.success("报价单提交成功");
        } else {
            return ApiResponse.error("报价单提交失败");
        }
    }

    /**
     * 保存草稿
     */
    @PostMapping("/draft")
    public ApiResponse<Quotation> saveDraft(@Valid @RequestBody Quotation quotation) {
        Quotation result = quotationService.saveDraft(quotation);
        return ApiResponse.success("草稿保存成功", result);
    }

    /**
     * 获取客户选项列表
     */
    @GetMapping("/customer-options")
    public ApiResponse<List<OptionDTO>> getCustomerOptions() {
        List<OptionDTO> options = quotationService.getCustomerOptions();
        return ApiResponse.success(options);
    }

    /**
     * 获取业务员选项列表
     */
    @GetMapping("/employee-options")
    public ApiResponse<List<OptionDTO>> getEmployeeOptions() {
        List<OptionDTO> options = quotationService.getEmployeeOptions();
        return ApiResponse.success(options);
    }

    /**
     * 更新报价单状态为已入库
     */
    @PutMapping("/{id}/status")
    public ApiResponse<String> updateQuotationStatus(@PathVariable Long id, @RequestBody java.util.Map<String, String> request) {
        String status = request.get("status");
        if ("已入库".equals(status)) {
            boolean result = quotationService.updateStatusToWarehoused(id);
            if (result) {
                return ApiResponse.success("报价单状态更新成功");
            } else {
                return ApiResponse.error("报价单状态更新失败");
            }
        } else {
            return ApiResponse.error("不支持的状态更新操作");
        }
    }
} 